<?php


/**
 * 73YC 书城
 * ============================================================================
 * * 版权所有 20013-* 梅州络客网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.alork.com；
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和
 * 使用；不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * $Author: liubo $
 * $Id: index.php 17217 2013-10-19 06:29:08Z liubo $
*/

define('IN_ECS', true);
require(dirname(__FILE__) . '/includes/init.php');
// 判断调试模式是否开启缓存
if ((DEBUG_MODE & 2) != 2)
{
    $smarty->caching = true;
}




$cache_id = sprintf('%X', crc32('bookstore'. '-'  . $_CFG['lang']));
if (!$smarty->is_cached('bookstore.dwt', $cache_id))
{   

assign_template();
$position = assign_ur_here();

/* 初始化分页信息 */
$page = isset($_REQUEST['page'])   && intval($_REQUEST['page'])  > 0 ? intval($_REQUEST['page'])  : 1;
$size = isset($_CFG['page_size'])  && intval($_CFG['page_size']) > 0 ? intval($_CFG['page_size']) : 8;

$record_count = $db->getOne("SELECT COUNT(*) FROM " .$ecs->table('goods'). " WHERE is_on_sale = 1 AND is_alone_sale = 1 AND is_delete = 0 AND is_new > 0 AND cat_id >= 602 AND cat_id <= 609 ");    //获取新书合计
$pager  = get_pager('bookstore.php', array('act' => $action), $record_count, $page ,8); //获取分页信息



    /* 获得请求的分类 ID */
    if (isset($_REQUEST['id'])){
        $cat_id = intval($_REQUEST['id']);
    }else{
        $cat_id = 601;
    }
    
    /* 获取分类书籍 */
    if($cat_id==601){
        $cate_list = bookstore_get_categoods(get_children($cat_id), "0", "0", "0", "", "12", "1", "sort_order","ASC");
    }else{
        $cate_list = bookstore_get_categoods(get_children($cat_id), "0", "0", "0", "", "12", "1", "goods_id","ASC");
    }
    $cate_list = array_values($cate_list);
    foreach($cate_list AS $key=>$value){
        $list[($key+1)]=$value;
    }
    $cate_list = $list;
    unset($list);

    if(!$cate_list) header('location:index.php');
    $smarty->assign('cate_list',$cate_list);    //获取分类书籍

    /* 获取nav表中的参数 */
    $navigator = get_navigator($ctype, $catlist);
    
    $smarty->assign('navigator', $navigator); //获取nav表中的参数
    $smarty->assign('pager', $pager); //分页信息
    $smarty->assign('page_title',       $position['title']);    // 页面标题


    /* 获取新书上架 */
    $newgoods_list = get_newgoods($pager['size'], $pager['start']);
    $smarty->assign('newgoods_list', $newgoods_list);

    /* 获取第书城头部大广告位的广告 */
    foreach (get_ad(13) as $key => $value) {
        $ad[13][$key] = $value;
        $ad[13][$key]['key'] = $key+1;
    }
    $ad_1=get_ad(14);//书城小广告1
    $ad_2=get_ad(15);//书城小广告2
    $ad_3=get_ad(16);//书城小广告3  

    $smarty->assign('ad', $ad);         // 广告列表
    $smarty->assign('ad_1', $ad_1);     //书城小广告1
    $smarty->assign('ad_2', $ad_2);     //书城小广告2
    $smarty->assign('ad_3', $ad_3);     //书城小广告3
    $smarty->assign('is_buy',       is_buy());
    $smarty->assign('script_name', 'bookstore');
    $smarty->assign('hot_goods',       get_recommend_goods('hot'));



    /* 获取所有书列表 */
    $goods_list = category_get_goods(get_children(601), "0", "0", "0", "", "10000", "1", "sort_order","DESC");
    /* 各类书列表 */
    /* 新品上架 */
    $new_goods_list = array(
        array(1199,1216,1223,1233,1169,1226,1232,1177,),
        array(1168,1198,1205,1229,1231,1212,1210,1193,),
        array(1188,1175,1196,1214,1217,1181,1176,1235,),
        );
    /* 热销榜 */
    $hot_goods_list = array(1199,1223,1169,1177,1217,1212,1214,1232,1190,1175,);
    /* 人气榜 */
    $click_goods_list = array(1176,1179,1197,1203,1205,1211,1207,1221,1234,1241,);
    /* 画种分类 */
    $cate_goods_list = array(
        array(1236,1222,1216,1189,1235,1189,1186,1182,1179,1177,1176,1178,),
        array(1234,1228,1227,1224,1221,1199,1198,1197,1196,1195,1170,1240,),
        array(1237,1231,1226,1223,1217,1194,1193,1192,1191,1190,1169,1168,),
        array(1239,1233,1209,1208,1238,1215,1212,1201,1241,1230,1210,1202,),
    );
    /* 系列丛书 */
    $series_goods_list = array(
        array(1180,1199,1200,1206,1210,1194,),
        array(1232,1231,1233,1236,1237,1239,),
        array(1212,1213,1215,1216,1217,1210,),
        array(1173,1169,1168,1175,1174,1177,),
        array(1221,1222,1223,1224,1225,1208,),
        array(1667,1660,1662,1664,1665,1666,),
        );
    /* 最下端展位 */
    $foot_goods_list = array(1169,1217,1224,1226,1232,);
    $smarty->assign('new_goods_list',        id_get_goods($new_goods_list,$goods_list));
    $smarty->assign('hot_goods_list',        get_key(id_get_goods($hot_goods_list,$goods_list)));
    $smarty->assign('click_goods_list',      get_key(id_get_goods($click_goods_list,$goods_list)));
    $smarty->assign('cate_goods_list',       id_get_goods($cate_goods_list,$goods_list));
    $smarty->assign('series_goods_list',     id_get_goods($series_goods_list,$goods_list));
    $smarty->assign('foot_goods_list',       id_get_goods($foot_goods_list,$goods_list));
}

    $smarty->display('bookstore.dwt', $cache_id);

/*------------------------------------------------------ */
//-- PRIVATE FUNCTION
/*------------------------------------------------------ */

/**
 * 获得新书上架商品
 *
 * @access  private
 * @return  array
 */
function get_newgoods($num = 8, $start = 0){
    $where = "g.is_on_sale = 1 AND g.is_alone_sale = 1 AND ".
            "g.is_delete = 0 AND g.is_new > 0 AND g.cat_id >= 602 AND g.cat_id <= 609 ";

    $sql = "SELECT g.goods_id, g.goods_name, g.shop_price, g.preferential_price5, g.goods_name_style, g.promote_price, g.promote_start_date, g.promote_end_date, g.goods_thumb, g.goods_img, g.goods_type, g.is_hot " . 
        " FROM " . $GLOBALS['ecs']->table('goods') . " AS g " . 
        " WHERE $where ORDER BY g.sort_order ASC ";
        $res = $GLOBALS['db']->selectLimit($sql, $num, $start);

    $arr = array();
    while ($row = $GLOBALS['db']->fetchRow($res))
    {
        if ($row['promote_price'] > 0)
        {
            $promote_price = bargain_price($row['promote_price'], $row['promote_start_date'], $row['promote_end_date']);
        }
        else
        {
            $promote_price = 0;
        }


        $arr[$row['goods_id']]['goods_id']         = $row['goods_id'];
        if($display == 'grid')
        {
            $arr[$row['goods_id']]['goods_name']       = $GLOBALS['_CFG']['goods_name_length'] > 0 ? sub_str($row['goods_name'], $GLOBALS['_CFG']['goods_name_length']) : $row['goods_name'];
        }
        else
        {
            $arr[$row['goods_id']]['goods_name']       = $row['goods_name'];
        }
        $arr[$row['goods_id']]['name']             = $row['goods_name'];
        $arr[$row['goods_id']]['goods_style_name'] = add_style($row['goods_name'],$row['goods_name_style']);
        $arr[$row['goods_id']]['shop_price']       = price_format($row['shop_price']);
        $arr[$row['goods_id']]['preferential_price5']  = ($row['preferential_price5'] > 0) ? price_format($row['preferential_price5']) : ''; //第五梯度价格
        $arr[$row['goods_id']]['type']             = $row['goods_type'];
        $arr[$row['goods_id']]['promote_price']    = ($promote_price > 0) ? price_format($promote_price) : '';
        $arr[$row['goods_id']]['goods_thumb']      = get_image_path($row['goods_id'], $row['goods_thumb'], true);
        $arr[$row['goods_id']]['goods_img']        = get_image_path($row['goods_id'], $row['goods_img']);
        $arr[$row['goods_id']]['url']              = build_uri('goods', array('gid'=>$row['goods_id']), $row['goods_name']);
        $arr[$row['goods_id']]['is_hot']         = $row['is_hot'];
    }
    
    return $arr;
}

/**
 * 获得广告位
 *
 * @access  private
 * @return  array
 */
function get_ad($position_id = 0){
        if(!$position_id) return false;
        $position_id = intval($position_id);
        $sql = "SELECT * FROM " . $GLOBALS['ecs']->table('ad') . " WHERE position_id = '$position_id' ORDER BY ad_name ASC ";
        return $GLOBALS['db']->getAll($sql);
    }


/**
 * 获得书城分类商品
 *
 * @access  private
 * @return  array
 */
function bookstore_get_categoods($children, $brand, $min, $max, $ext, $size, $page, $sort, $order)
{
    $display = $GLOBALS['display'];
    $where = "g.is_on_sale = 1 AND g.is_alone_sale = 1 AND ".
            "g.is_delete = 0 AND ($children OR " . get_extension_goods($children) . ')';
    
    if ($brand > 0)
    {
        $where .=  "AND g.brand_id=$brand ";
    }

    if ($min > 0)
    {
        $where .= " AND g.shop_price >= $min ";
    }

    if ($max > 0)
    {
        $where .= " AND g.shop_price <= $max ";
    }
    
    /* 获得商品列表 */
    $sql = "SELECT g.goods_id, g.goods_name, g.shop_price, g.preferential_price5, g.goods_name_style, g.promote_price, g.promote_start_date, g.promote_end_date, g.goods_thumb, g.goods_img, g.goods_type, g.is_hot " . 
        " FROM " . $GLOBALS['ecs']->table('goods') . " AS g " . 
        " WHERE $where ORDER BY g.sort_order ASC ";
    $res = $GLOBALS['db']->selectLimit($sql, $size, ($page - 1) * $size);
    $arr = array();
    while ($row = $GLOBALS['db']->fetchRow($res))
    {
        if ($row['promote_price'] > 0)
        {
            $promote_price = bargain_price($row['promote_price'], $row['promote_start_date'], $row['promote_end_date']);
        }
        else
        {
            $promote_price = 0;
        }

        $arr[$row['goods_id']]['goods_id']         = $row['goods_id'];
        $arr[$row['goods_id']]['goods_name']       = $row['goods_name'];
        $arr[$row['goods_id']]['goods_style_name'] = add_style($row['goods_name'],$row['goods_name_style']);
        $arr[$row['goods_id']]['shop_price']       = price_format($row['shop_price']);
        $arr[$row['goods_id']]['preferential_price5']  = ($row['preferential_price5'] > 0) ? price_format($row['preferential_price5']) : ''; //第五梯度价格
        $arr[$row['goods_id']]['type']             = $row['goods_type'];
        $arr[$row['goods_id']]['promote_price']    = ($promote_price > 0) ? price_format($promote_price) : '';
        $arr[$row['goods_id']]['goods_thumb']      = get_image_path($row['goods_id'], $row['goods_thumb'], true);
        $arr[$row['goods_id']]['goods_img']        = get_image_path($row['goods_id'], $row['goods_img']);
        $arr[$row['goods_id']]['url']              = build_uri('goods', array('gid'=>$row['goods_id']), $row['goods_name']);
        $arr[$row['goods_id']]['is_hot']         = $row['is_hot'];
    }
    return $arr;
}

//产品列表
function category_get_goods($children, $brand, $min, $max, $ext, $size, $page, $sort, $order)
{   
    $where = "g.is_on_sale = 1 AND g.is_alone_sale = 1 AND ".
            "g.is_delete = 0 AND ($children OR " . get_extension_goods($children) . ')';

    /* 获得商品列表 */
    $sql = 'SELECT g.goods_id, g.shop_price, g.goods_name,g.promote_price, g.promote_start_date, g.promote_end_date, g.goods_thumb, g.is_hot ' .
            'FROM ' . $GLOBALS['ecs']->table('goods') . ' AS g ' .
            "WHERE $where $ext ORDER BY $sort $order,goods_id DESC";

    $res = $GLOBALS['db']->selectLimit($sql, $size, ($page - 1) * $size);
    $arr = array();
    while ($row = $GLOBALS['db']->fetchRow($res))
    {
        if ($row['promote_price'] > 0)
        {
            $promote_price = bargain_price($row['promote_price'], $row['promote_start_date'], $row['promote_end_date']);
        }
        else
        {
            $promote_price = 0;
        }

        $arr[$row['goods_id']]['goods_id']             = $row['goods_id'];
        $arr[$row['goods_id']]['goods_name']                 = $row['goods_name'];
        $arr[$row['goods_id']]['is_hot']               = $row['is_hot'];
        $arr[$row['goods_id']]['shop_price']           = format_price($row['shop_price']);
        if($promote_price){
            $arr[$row['goods_id']]['promote_price']    = format_price($row['promote_price']);
        }else{
            $arr[$row['goods_id']]['promote_price']    = '';
        }
        $arr[$row['goods_id']]['goods_thumb']          = get_image_path($row['goods_id'], $row['goods_thumb'], true);
        $arr[$row['goods_id']]['url']                  = build_uri('goods', array('gid'=>$row['goods_id']), $row['goods_name']);
    }

    return $arr;
}

/* 用ID获取商品列表 */
function id_get_goods($id_array,$goods_list){
    $data = array();
    if(is_array($id_array) && $id_array){
        foreach ($id_array as $key => $value) {
            if(is_array($value) && $value){
                foreach ($value as $k => $v) {
                    if($v){
                        $data[$key][$k] = $goods_list[$v];
                    }
                }
            }else{
                if($value){
                    $data[$key] = $goods_list[$value];
                }
            }
        }
    }
    return $data;
}

function get_key($data){
    foreach ($data as $key => $value) {
        $datas[$key+1]=$value;
    }
    return $datas;
}
?>
